package hu.bme.aut.consulttime.client.components;

import hu.bme.aut.consulttime.client.StudentTeamMembershipService;
import hu.bme.aut.consulttime.client.StudentTeamMembershipServiceAsync;
import hu.bme.aut.consulttime.entitydtos.StudentTeamMembershipDTO;
import hu.bme.aut.consulttime.entitydtos.TeamDTO;
import hu.bme.aut.consulttime.entitydtos.UserDTO;
import hu.bme.aut.consulttime.enums.WeekDay;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;

public class ModifyConsultTime extends DialogBox {
	
	private StudentTeamMembershipServiceAsync stmService = GWT
			.create(StudentTeamMembershipService.class);
	
	public ModifyConsultTime(final TeamDTO teamDTO, List<StudentTeamMembershipDTO> smtList) {
		super(true);
		FlexTable flexTable = new FlexTable();
		flexTable.setBorderWidth(1);

		// Add these lines
		flexTable.setText(0, 0, "Member");
		flexTable.setText(0, 1, "ConsultDay");
		flexTable.setText(0, 2, "ConsultTime");		
		flexTable.setText(0, 3, "Modify");		
		
		for(int i=0 ; i < teamDTO.getMembers().size(); ++i){
			//----- UserName
			final UserDTO tmpStudent = teamDTO.getMembers().get(i);
			Label uNameLabel = new Label(tmpStudent.getFirstName() + " " + tmpStudent.getLastName() + " (" + tmpStudent.getUserName() + ")");
			flexTable.setWidget(i+2, 0, uNameLabel);
			
			//----- ConsultDay
			final ListBox comboBoxDay = new ListBox();
			flexTable.setWidget(i+2, 1, comboBoxDay);
			comboBoxDay.setWidth("100%");
			
			WeekDay[] values = WeekDay.values();
			for (int j = 0; j < values.length; ++j) {
				comboBoxDay.addItem(values[j].toString(), values[j].name());
			}
			
			// set default
			WeekDay cDay = getSTMDTO(tmpStudent.getUserId(), smtList)
					.getConsultDay();
			if (cDay != null) {
				String consultDay = cDay.toString();
				for (int j = 0; j < comboBoxDay.getItemCount(); ++j) {
					if (comboBoxDay.getItemText(j).equals(consultDay)) {
						comboBoxDay.setSelectedIndex(j);
					}
				}
			}

			//----- ConsultTime
			final TimeBox timeBox = new TimeBox();
			flexTable.setWidget(i+2, 2, timeBox);
			
			// set default
			timeBox.setDate(getSTMDTO(tmpStudent.getUserId(), smtList)
					.getConsultTime());
			
			Button modifyButton = new Button("Modify");
			modifyButton.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					
					StudentTeamMembershipDTO stmDTO = new StudentTeamMembershipDTO();
					stmDTO.setTeamId(teamDTO.getTeamId());
					stmDTO.setStudentId(tmpStudent.getUserId());
					
					WeekDay wd = WeekDay.valueOf(comboBoxDay.getValue(comboBoxDay.getSelectedIndex()));
					stmDTO.setConsultDay(wd);
					stmDTO.setConsultTime(timeBox.getDate());
					//System.out.println(stmDTO.getConsultTime());
					
					stmService.modifyConsultTime(stmDTO, new AsyncCallback<Boolean>() {
						
						@Override
						public void onSuccess(Boolean result) {
							if(result == true){
								Window.confirm("'"+tmpStudent.getUserName() + "' consultTime modified!");
						        //Refresh the table
						      //  ProjectPage.loadProjects();
							}else{
								Window.alert("Project modification failed!");
							}
						}
						
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Remote Procedure Call - Failure");
						}
					});
				}
			});
			flexTable.setWidget(i+2, 3, modifyButton);
		}
		setWidget(flexTable);
	}
	
	private StudentTeamMembershipDTO getSTMDTO(long studentId, List<StudentTeamMembershipDTO> smtList){
		for(StudentTeamMembershipDTO tmp : smtList){
			if(tmp.getStudentId() == studentId){
				return tmp;
			}
		}
		return null;
	}
}
